#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "Car.h"
#include "Key.h"
#include "HCSR04.h"
#include "Servo.h"
#include "Serial.h"
#include "Track.h"

uint8_t KeyNum;
int8_t Speed;
float Dis1=16;
float Dis2;
float Dis3;
float Angle;
uint16_t Data1;
uint8_t State;
uint8_t SensorState[4];
uint8_t Area;
uint8_t Last_Area;
uint8_t angle;
uint8_t j;

int main(void)
{
	
	OLED_Init();			//显示器
	Car_Init();				//小车
	Servo_Init();			//舵机
	Serial_Init();			//串口
	Sensor_Init();			//红外传感器
	Car_Stop();				//小车初始静止
	SR04_Init();			//超声波初始化
	
	OLED_ShowString(1, 1, "Mode:");
	Servo_SetAngle(90);
	
	while (1)
	{	
		//控制小车状态:0-蓝牙模式 1-自动避障 2-红外循迹
		
		if(State == 0)			//蓝牙模式
		{
			OLED_ShowString(1, 6, "Bluetooth");
			OLED_ShowString(2, 1, "                ");
		}
		
		if(State == 1)			//自动避障模式
		{
			OLED_ShowString(1, 6, "Avoidance");
			OLED_ShowString(2, 1, "SRDis:");
			OLED_ShowString(2, 10, ".");
			OLED_ShowString(2, 13, "cm");
			for(j=0;j<3;j++)
			{
				Show_SRDis();//超声波显示当前距离
			}
			Dis1=Get_DisVal();//获取当前超声波测得距离
			{
				if(Dis1>20)			//如果前方距离大于15，前进
				{
					Go_Ahead_1();
				}
				else						//如果前方距离小于15CM
				{
					Car_Stop();
					Servo_SetAngle(180);
					for(j=0;j<3;j++)
					{
						Show_SRDis();//超声波显示当前距离
					}
					Dis2= Get_DisVal();		//调整舵机角度向左转,获取距离
					if(Dis2>20)					//如果左边距离大于15CM
					{	
						Servo_SetAngle(90);		//调整舵机角度向正前方,同时原地左转,前进
						Delay_ms(1000);
						Self_Left();
						Delay_ms(1000);
						Go_Ahead_1();
					}
					else
					{
						Servo_SetAngle(0);	//调整舵机角度向右转,获取距离
						Delay_ms(1000);
						for(j=0;j<3;j++)
						{
							Show_SRDis();//超声波显示当前距离
						}
						Dis3= Get_DisVal();
						if(Dis3>20)				//如果右边距离大于15CM
						{	
							Servo_SetAngle(90);
							Delay_ms(1000);
							Self_Right();
							Delay_ms(1000);
							Go_Ahead_1();				//调整舵机角度向正前方,同时原地右转,前进
						}
						else					//如果右边距离小于15CM
						{
							Servo_SetAngle(90);
							Car_Stop();
							while(1)
							{
									
							}			//调整舵机角度向正前方，停止不动
						}
					}
				}
			}
		}
		
		if(State == 2)			//红外循迹模式
		{
			OLED_ShowString(1, 6, "Tracking");
			OLED_ShowString(2, 1, "                ");
			SensorState[0] = Sensor0_GetState();
			SensorState[1] = Sensor1_GetState();
			SensorState[2] = Sensor2_GetState();
			SensorState[3] = Sensor3_GetState();
			
			if(!SensorState[0]&&SensorState[1]&&SensorState[2]&&!SensorState[3])	//Area3,直线
			{
				Area = 3;
				Go_Ahead();
			}
			else if(!SensorState[0]&&SensorState[1]&!SensorState[2]&&!SensorState[3])	//Area4,右小弯
			{
				Area = 4;
				My_Turn_Right(50);
			}
			else if(!SensorState[0]&&!SensorState[1]&&!SensorState[2]&&SensorState[3])	//Area6,右大弯
			{
				Area = 6;
				My_Turn_Right(80);
			}
			else if(SensorState[0]&&!SensorState[1]&&!SensorState[2]&&!SensorState[3])	//Area0,左大弯
			{
				Area = 0;
				My_Turn_Left(80);
			}
			else if(!SensorState[0]&&SensorState[1]&&!SensorState[2]&&!SensorState[3])	//Area2,左小弯
			{
				Area = 2;
				My_Turn_Left(50);
			}
			else if(!SensorState[0]&&!SensorState[1]&&!SensorState[2]&&!SensorState[3])	//中弯
			{
				if(Last_Area == 2)
				{
					Area = 1;
					My_Turn_Left(60);
				}
				else if(Last_Area == 4)
				{
					Area = 5;
					My_Turn_Right(60);
				}
			}
			
			Last_Area = Area;
			Delay_ms(30);
		}
		
   }
}
	
void USART1_IRQHandler(void)
{
	if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
	{
		Data1=USART_ReceiveData(USART1);
		if(Data1==0x37)State = 0;
		if(Data1==0x38)State = 1;
		if(Data1==0x39)State = 2;
		if(State == 0)				//蓝牙模式下才会执行以下命令
		{
			if(Data1==0x30)Car_Stop();
			if(Data1==0x31)Go_Ahead();
			if(Data1==0x32)Go_Back();
			if(Data1==0x33)Turn_Left();
			if(Data1==0x34)Turn_Right();
			if(Data1==0x35)Self_Left();
			if(Data1==0x36)Self_Right();
			USART_ClearITPendingBit(USART1, USART_IT_RXNE);
		}
	}
}
